<%inherit file="base.html"/>

<%def name="head()">
<title>在线用户管理</title>
<script>

var updater = {
    socket: null,
    start: function () { 
        var _prefix = "${use_ssl and 'wss' or 'ws'}";
        ws_addr = "${sys_param_value('radiusd_address')}";
        if("127.0.0.1" == ws_addr){
            ws_addr = location.hostname;
        }
        var url = _prefix+"://"+ws_addr+":${sys_param_value('radiusd_admin_port')}";
        updater.socket = new WebSocket(url);
        updater.socket.onmessage = function (event) {
            updater.showMessage(JSON.parse(event.data));
        }
        updater.socket.onclose = function (event) {
            updater.start();
        }
        updater.socket.onopen = updater.onOpen;
    },
    showMessage: function (message) {
        alert(message.data)
        window.location.reload();
    },

    onOpen: function(event){
    },
    unlock_online: function(nas_addr,session_id){
        var message = {
            process : "admin_unlock_online",
            nas_addr : nas_addr,
            acct_session_id : session_id
        };
        data = JSON.stringify(message);
        $.post("/encrypt", {data:data} , function (ev) {
            updater.socket.send(ev.data);
        }, "json");
    },
    disconnect_online: function(nas_addr,session_id){
        var message = {
            process : "admin_coa_request",
            nas_addr : nas_addr,
            acct_session_id : session_id,
            message_type : 'disconnect'
        };
        data = JSON.stringify(message);
        $.post("/encrypt", {data:data} , function (ev) {
            updater.socket.send(ev.data);
        }, "json");
    }
};

$(document).ready(function (){
    updater.start();
    $("#realseAll").click(function(){
        var nas_addr = $("#nas_addr").val();
        if(nas_addr.length == 0)
        {
            alert("请选择bas");
            return false;
        }
        if(confirm("是否解锁该bas下所有用户?"))
        {
             updater.unlock_online(nas_addr,'')
        }
    });

});

</script>

</%def>

<%def name="body()">
<section class="content">
    <div class="box">
    <div class="panel-heading"><i class="fa fa-users"></i> 在线用户管理</div>
    <div class="panel-body">
        <form id="query_form" class="form-horizontal form-well" role="form" action="/online/query" method="post">
            <div class="form-group">
                <label for="node_id" class="col-md-2 control-label">区域</label>
                <div class="col-md-3">
                    <select id="node_id" name="node_id" class="form-control" onchange="whenNodeChange()">
                        <option></option>
                        % for node in node_list:
                          <option value="${node.id}" ${str(node.id)==node_id and "selected" or ""}>${node.node_desc}</option>
                        % endfor
                    </select>
                </div>
                <label for="account_number" class="col-md-2 control-label">用户账号</label>
                <div class="col-md-3">
                    <input type="text" id="account_number" name="account_number" class="form-control" placeholder="模糊查询" value=${account_number or ""} >
                </div>
            </div>

            <div class="form-group">
                 <label for="framed_ipaddr" class="col-md-2 control-label">IP地址</label>
                <div class="col-md-3">
                    <input id="framed_ipaddr"  name="framed_ipaddr" class="form-control" type="text" value=${ip_addr or ""}>
                </div>


                <label for="mac_addr" class="col-md-2 control-label">MAC地址</label>
                <div class="col-md-3">
                    <input type="text" id="mac_addr" name="mac_addr" class="form-control" value=${mac_addr or ""}>
                </div>
            </div>

            <div class="form-group">

                <label for="nas_addr" class="col-md-2 control-label">选择bas</label>
                <div class="col-md-3">
                     <select id="nas_addr" name="nas_addr" class="form-control">
                        <option></option>
                        % for bas in bas_list:
                          <option value="${bas.ip_addr}" ${bas.ip_addr== nas_addr and "selected" or ""}>${bas.bas_name}</option>
                        % endfor
                    </select>
                </div>

            </div>

            <div class="form-group">
                <div class="col-md-offset-5 col-md-7">
                    <button type="submit" class="btn btn-primary">查询</button>&nbsp;&nbsp;
                    % if get_cookie("opr_type") == 0 :
                    <button type="button" class="btn btn-default" id="realseAll">BAS用户下线</button>
                    % endif
                </div>
            </div>
        </form>


        <table class="table table-hover">
            <thead>
            <tr>
                <th>用户姓名</th> 
                <th>用户账号</th>
                <th>BAS地址</th>
                <th>上线时间</th>
                <th>IP地址</th>
                <th>MAC地址</th>
                <th>在线时间</th>
                <th>上传/下载</th>
                <th></th>
            </tr>
            </thead>

            <tbody>
                % for online in page_data.result:
            <tr>
                <td>${online.realname}</td>
                <td>${online.account_number}</td>
                <td>${online.nas_addr}</td>
                <td>${online.acct_start_time}</td>
                <td>${online.framed_ipaddr}</td>
                <td>${online.mac_addr}</td>
                <td>${fmt_online_time(online.acct_start_time)}</td>
                <td>${kb2mb(online.input_total)}M/${kb2mb(online.output_total)}M</td>
                <td>
                <a class="opt-btn btn-default" href="javascript:updater.unlock_online('${online.nas_addr}','${online.acct_session_id}');">解锁</a>
                <a class="opt-btn btn-default" href="javascript:updater.disconnect_online('${online.nas_addr}','${online.acct_session_id}');">强制下线</a>
                </td>
            </tr>
                % endfor
            </tbody>

        </table>
        ${page_data.render(form_id="query_form")}

    </div>
</div>
</section>
</%def>
